'''
Created on May 7, 2011

@author: yc30v1m
'''
from library.AutoLoad import AutoLoad
from library.Configuration import Configuration
from plugins.testlink.Client import Client
from logging import getLogger

log = getLogger(__name__)

class Testlink(object):
    '''
    classdocs
    '''
    _confReader = ""
    __testLinkPlugin = ""
    _testLinkSetup = ""
    _testCases = {}
    

    def __init__(self,setUpInfo):
        self._confReader = Configuration()
        self.__createClient()
        self.__readAllTestCases()
        
    def getTestCases(self):
        return self._testCases
        
    def __readAllTestCases(self):
        try:
            tcids = self.__testLinkPlugin.getTestCaseIdsForTestPlan()
            tc = {}
            for tcid in tcids:
                try:
                    testCase = {}
                    testCase = self.__testLinkPlugin.getAutomationTestCaseFromAttachment(tcid)
                    if testCase == False:
                        #continue with next TestCase ID
                        continue
                    testCase['TESTCASE_ID'] = tcid
                    if len(testCase) > 0:
                        tc[tcid]=testCase.copy()
                        del testCase
                except :
                    log.warning( "Exception Occured in TCID :TestLINK read _readAllTestCases " + tcid)
            #End For 
            self._testCases = tc


        except :
            log.critical("Error occured")

    def __createClient(self):
        try:
            apikey = self._confReader.getOptionFromSection('testcases_testlink', 'apikey')
            url = self._confReader.getOptionFromSection('testcases_testlink', 'url')
            testProject = self._confReader.getOptionFromSection('testcases_testlink', 'project_name')
            testProjectPrefix = self._confReader.getOptionFromSection('testcases_testlink', 'project_prefix')
            testPlan = self._confReader.getOptionFromSection('testcases_testlink', 'testplan')
            #TlClient = AutoLoad.ClassOnly('plugins.testlink.Client')
            self.__testLinkPlugin = Client(apikey,testProject,testPlan, url, testProjectPrefix)

        except:
            log.critical("TestLink aggregator plugin setup problem occured")
            AutoLoad.logExceptionInfo()
        